//
//  MZYbrandinfoViewController.m
//  WristWatch
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYbrandinfoViewController.h"
#import "MZYHeader.h"
#import "MZYstyleModel.h"
#import "MZYsecondTableViewCell.h"
#import "MZYthirdViewController.h"

@interface MZYbrandinfoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, assign)NSInteger page;

@end

@implementation MZYbrandinfoViewController

- (void)dealloc {
    [_tableView release];
    [_arr release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arr = [NSMutableArray array];
    [self createView];
    [self createData];
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    self.tableView.rowHeight = 120;
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
//    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:86 / 255.0 green:137 / 255.0 blue:157 / 255.0 alpha:1]];
    //下拉
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
    //上啦
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
    
    self.page = 1;
    
    
    // 建立小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.detailsLabelText = @"请稍后";
    self.HUD.dimBackground = YES;


}

#pragma mark 上拉下拉方法
//下拉
- (void)header {
    //把已经刷新的数据删除,否则越刷新重复数据越多
    [self createData];
    
}
//上拉
-(void)footer {
//    self.page += 2;
    self.page ++;
    NSLog(@"%ld",(long)self.page);
    NSString *url = [NSString stringWithFormat:@"http://api.ttime.com/searchApi/list?token=f1cfa3eafd64a3474df1d5567d830da6&page=%ld&brand=%@",(long)self.page, self.brand];
    NSLog(@"000000 %@",url);
    NSMutableArray *tempArr = [NSMutableArray array];
    // 解析UITableView
    AFHTTPRequestOperationManager *Tabmanager = [AFHTTPRequestOperationManager manager];
    [Tabmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *zArr = dic[@"result"][@"goodsDate"];
        NSLog(@"%ld",zArr.count);
//        for (NSDictionary *tempDic in zArr) {
//            MZYInformationModel *info = [[MZYInformationModel alloc] init];
//            [info setValuesForKeysWithDictionary:tempDic];
//             [self.arr addObject:info];
//            NSLog(@"????????%ld",(unsigned long)self.arr.count);
//            [info release];
//        }
        NSMutableArray *tempArr = [MZYInformationModel MZYBaseModelByArr:zArr];
        [self.arr arrayByAddingObjectsFromArray:tempArr];
        NSLog(@"?????????????%ld",self.arr.count);
        
        if (self.arr.count > 0) {
            self.HUD.hidden = YES;
            [self.tableView reloadData];
        }
        
        [self.tableView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)createData {
    // 解析UITableView
    AFHTTPRequestOperationManager *Tabmanager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://api.ttime.com/searchApi/list?token=f1cfa3eafd64a3474df1d5567d830da6&page=1&brand=%@",self.brand];
    [Tabmanager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *zArr = dic[@"result"][@"goodsDate"];
        for (NSDictionary *tempDic in zArr) {
            MZYstyleModel *style = [[MZYstyleModel alloc] init];
            [style setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject:style];
//            NSLog(@"%@", style);
            [style release];
        }
        if (self.arr.count > 0) {
            self.HUD.hidden = YES;
            [self.tableView reloadData];
        }
        [self.tableView.header endRefreshing];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}

#pragma mark tableView的协议方法
// tableView协议必须实现的第一个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
    
}
// tableView协议必须实现的第二个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    MZYsecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[MZYsecondTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    MZYstyleModel *style = self.arr[indexPath.row];
    cell.nameLabel.text = style.name;
    NSString *str = @"中国零售价";
    cell.priceLabel.text = [NSString stringWithFormat:@"%@:  %@", str, style.price];
    [cell.nameImageView sd_setImageWithURL:[NSURL URLWithString:style.image] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MZYstyleModel *model = self.arr[indexPath.row];
    MZYthirdViewController *thiVC = [[MZYthirdViewController alloc] init];
    thiVC.style = model;
    thiVC.product_id = model.product_id;
    thiVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:thiVC animated:YES];
    [thiVC release];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
